# Gilardi, Fabrizio, "The Temporary Importance of Role Models for Women's Political Representation", American Journal of Political Science
# Code to replicate Figure SI3 (Geographic distribution of the percent of women candidates, 1970–2010)
# gilardi@ipz.uzh.ch, 2014-06-24

# Set working directory
setwd("../Data/")

# Load packages
library(lattice)
library(sp)

# Load data

d <- read.csv("dataset-full.csv")

bfs <- read.csv("bfs.csv", encoding="UTF-8")

load("CHE_adm2.RData")
ZH <- gadm[gadm$NAME_2=="Zürich",]

load("CHE_adm3.RData")
gadm.zh.0 <- gadm[gadm$ID_1==26,] 

# names(gadm.zh)
# gadm.zh$ISO		--> Country acronym
# gadm.zh$NAME_0	--> Country name
# gadm.zh$ID_1		--> canton ID
# gadm.zh$NAME_1	--> canton name
# gadm.zh$ID_2		--> Bezirk ID
# gadm.zh$NAME_2	--> Bezirk name
# gadm.zh$ID_3		--> Units ID (Gemeinden + lakes + ZH Kreise etc.)
# gadm.zh$NAME_3	--> Units names (Gemeinden + lakes + ZH Kreise etc.)
# gadm.zh$TYPE_3	--> Type of unit ("Commune" or "Water body")
# gadm.zh$ENGTYPE_3	--> Type of unit ("Municipality" or "Water body")



# Prepare stuff

ZH <- ZH[, 1:8]
gadm.zh.0 <- gadm.zh.0[,-c(11:12)]
names(ZH)
names(gadm.zh.0)

ZH$ID_3 <- NA
ZH$NAME_3 <- NA
ZH$TYPE_3 <- NA
ZH$ENGTYPE_3 <- NA

gadm.zh <- rbind(gadm.zh.0, ZH)
gadm.zh$NAME_3[is.na(gadm.zh$NAME_3)==T] <- c("Zürich")
gadm.zh$TYPE_3[is.na(gadm.zh$TYPE_3)==T] <- c("Commune")
gadm.zh$ENGTYPE_3[is.na(gadm.zh$ENGTYPE_3)==T] <- c("Municipality")

gadm.zh <- gadm.zh[(gadm.zh$NAME_3=="Zürich" | gadm.zh$NAME_2!="Zürich"),]
gadm.zh <- gadm.zh[gadm.zh$NAME_2!="Schaffhausen",]
gadm.zh <- gadm.zh[gadm.zh$TYPE_3!="Water body",]

length(gadm.zh$NAME_3)==171

gadm.zh$NAME_3[gadm.zh$NAME_3=="Horgen-Sihlbrugg Dorf"] <- c("Horgen")
gadm.zh$NAME_3[gadm.zh$NAME_3=="Richterswil-Samstagern"] <- c("Richterswil")

gadm.zh$bfsnr <- NA

names <- unique(bfs$gde)
for(i in names){
	gadm.zh$bfsnr[gadm.zh$NAME_3==i] <- bfs$bfs[bfs$gde==i]
}

gadm.zh$bfsnr[gadm.zh$NAME_3=="Sternenberg"]
bfs$bfs[bfs$gde=="Sternenberg"]


# Attach percent of women for the various election years

y <- 1970
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.1970[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 1974
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.1974[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 1978
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.1978[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 1982
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.1982[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 1986
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.1986[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 1990
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.1990[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 1994
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.1994[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 1998
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.1998[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 2002
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.2002[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 2006
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.2006[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}

y <- 2010
for(i in unique(d$bfsnr[d$year==y])){
	gadm.zh$cand.2010[gadm.zh$bfsnr==i] <- d$pcent.women.cand[d$bfsnr==i & d$year==y]*100
}


# Set colors

ll <- length(unique(gadm.zh$bfsnr[is.na(gadm.zh$cand.2010)==F]))
col <- c()
jj <- seq(0.1, 1, length=ll)
for(i in 1:ll){
	col[i] <- hsv(h=0.9, s=1, v=1, alpha=jj[i])
}

col.bw <- c()
jj <- seq(0.1, 1, length=ll)
for(i in 1:ll){
	col.bw[i] <- hsv(h=0.2, s=0.2, v=0.2, alpha=jj[i])
}

# Vector of election years
yrs <- seq(1970, 2010, 4)

# Compute means
mean.cand <- c()
for(i in 1:length(yrs)){
	mean.cand[i] <-  round(mean(d$pcent.women.cand[d$year==yrs[i]], na.rm=T)*100, 1)
}


# Set names and labels

names.cand <- c(paste(yrs[1], " ", "(", mean.cand[1], ")", sep=""), paste(yrs[2], " ", "(", mean.cand[2], ")", sep=""), paste(yrs[3], " ", "(", mean.cand[3], ")", sep=""), paste(yrs[4], " ", "(", mean.cand[4], ")", sep=""), paste(yrs[5], " ", "(", mean.cand[5], ")", sep=""), paste(yrs[6], " ", "(", mean.cand[6], ")", sep=""), paste(yrs[7], " ", "(", mean.cand[7], ")", sep=""), paste(yrs[8], " ", "(", mean.cand[8], ")", sep=""), paste(yrs[9], " ", "(", mean.cand[9], ")", sep=""), paste(yrs[10], " ", "(", mean.cand[10], ")", sep=""), paste(yrs[11], " ", "(", mean.cand[11], ")", sep=""))


y.cand <- c("cand.1970", "cand.1974", "cand.1978", "cand.1982", "cand.1986", "cand.1990", "cand.1994", "cand.1998", "cand.2002", "cand.2006", "cand.2010")


# Figure

#pdf(file="Figure-SI3.pdf", paper="special", width=7, height=10)
spplot(gadm.zh, y.cand, col.regions=col.bw, names.attr=names.cand, at=seq(0,60,10), layout=c(3,4), panel.col="blue", as.table=T, par.strip.text=c(cex=0.9), par.settings=list(strip.background=list(col="white")))
#dev.off()





